﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Demo.AgilePlanner
{
    public partial class StoryShape
    {
        #region not used
        /*
        public override void OnShapeInserted()
        {
            base.OnShapeInserted();
            UpdateStoryContainerCapacity();

        }

        protected override void OnDeleted()
        {
            base.OnDeleted();
            UpdateStoryContainerCapacity();
        }

        private void UpdateStoryContainerCapacity()
        {
            foreach (ShapeElement shape in this.Diagram.NestedChildShapes)
            {
                if (shape is BaseSwimLane)
                {
                    ((BaseSwimLane)shape).UpdateStoryContainerCapacity();
                }
            }
        }

        private void UpdateStoryCapacity(float capacity)
        {
            Story story = ModelElement as Story;
            if (story != null)
            {
                Store.TransactionManager.BeginTransaction();
                story.Capacity = capacity;

                //story.StoryContainer.UpdateCapacity();
                Store.TransactionManager.CurrentTransaction.Commit();
            }
        }

        public void SetCapacityValue(float value)
        {
            UpdateStoryCapacity(value);
        }

        public float GetCapacityValue()
        {
            return ModelElement == null ? 0 : ((Story)ModelElement).Capacity;
        }
        */
        #endregion

        protected override void InitializeDecorators(IList<ShapeField> shapeFields, IList<Decorator> decorators)
        {
            base.InitializeDecorators(shapeFields, decorators);
            TextField field = (TextField)ShapeElement.FindShapeField(shapeFields, "NameDecorator");

            field.DefaultMultipleLine = true;
            field.DefaultAutoSize = false;

            field.DefaultStringFormat.Trimming = StringTrimming.EllipsisCharacter;
            field.DefaultStringFormat.LineAlignment = StringAlignment.Near;

            field.AnchoringBehavior.Clear();
            field.AnchoringBehavior.SetLeftAnchor(AnchoringBehavior.Edge.Left, 0.06);
            field.AnchoringBehavior.SetRightAnchor(AnchoringBehavior.Edge.Right, 0.06);
            field.AnchoringBehavior.SetTopAnchor(AnchoringBehavior.Edge.Top, 0.26);
            field.AnchoringBehavior.SetBottomAnchor(AnchoringBehavior.Edge.Bottom, 0.26);
        }

        public override void OnShapeInserted()
        {
            base.OnShapeInserted();
            UpdateFillColor();
        }
    }
    
    public partial class StoryShapeBase
    {
        public bool GetExpandedValue()
        {
            return AbsoluteBoundingBox.Height > 0.5;
        }

        public Status GetStatusValue()
        {
            Story story = this.ModelElement as Story;
            return story == null ? Status.Pending : story.Status;
        }

        public void SetStatusValue(Status value)
        {
            Story story = this.ModelElement as Story;
            if (story == null) return;
            using (Store.TransactionManager.BeginTransaction("set story status"))
            {
                story.Status = value;
                UpdateFillColor();
                Store.TransactionManager.CurrentTransaction.Commit();
            }
        }

        public Priority GetPriorityValue()
        {
            Story story = this.ModelElement as Story;
            return story == null ? Priority.Medium : story.Priority;
        }

        public void SetPriorityValue(Priority value)
        {
            Story story = this.ModelElement as Story;
            if (story == null) return;
            using (Store.TransactionManager.BeginTransaction("set story priority"))
            {
                story.Priority = value;
                UpdateFillColor();
                Store.TransactionManager.CurrentTransaction.Commit();
            }
        }

        public void UpdateFillColor()
        {
            using (Store.TransactionManager.BeginTransaction("set story fill color"))
            {
                FillColor = GetFillColor();
                Store.TransactionManager.CurrentTransaction.Commit();
            }
        }

        public Color GetFillColor()
        {
            Project project = this.Diagram.ModelElement as Project;
            Story story = this.ModelElement as Story;
            switch (project.ColorSchema)
            {
                case ColorSchema.Status:
                    switch (story.Status)
                    {
                        case Status.Pending:
                            return project.PendingColor;
                        case Status.InProgress:
                            return project.InProgressColor;
                        case Status.Completed:
                            return project.CompletedColor;
                    }
                    break;

                case ColorSchema.Priority:
                    switch (story.Priority)
                    {
                        case Priority.Low:
                            return project.LowColor;
                        case Priority.Medium:
                            return project.MediumColor;
                        case Priority.High:
                            return project.HighColor;
                    }
                    break;
            }
            return Color.White;
        }
    }
}
